<template>
  <div class="layout-col plr15 pb15">
    <!-- <div class="ww100 hidden flex-bc flex2 mb15">
      <div class="layout-col hidden flex1 mr15">
        <left-top1 class="ww100 hidden flex1 mb15" />
        <left-top1 class="ww100 hidden flex1 mb15" />
        <left-top1 class="ww100 hidden flex2" />
      </div>
      <top class="ww100 hidden flex3" />
    </div> -->
    <top class="ww100 hidden flex2 mb15" />
    <div class="ww100 hidden flex-bc flex1">
      <bottom-left class="flex1 mr15" />
      <bottom-right class="flex3" />
    </div>
  </div>
</template>

<script lang="ts" setup>
// import leftTop1 from "@/views/async/new/leftTop1";
import top from "@/views/async/new/top";
import bottomRight from "@/views/async/monitor/centerBottom";
import bottomLeft from "@/views/async/monitor/leftBottom";
import scheduled from "@/utils/scheduled";
const { createScheduled } = scheduled();
const route = useRoute();
const { proxy }: any = getCurrentInstance();
const publicStore = proxy.publicStore();
const configStore = proxy.configStore();
const state = reactive({
  ...publicStore.$state,
  voltageList: [],
  bayList: [],
  templateValue: [],
  params1: [],
  params2: [],
  params3: [],
  params4: [],
});

onMounted(async () => {
  await setInit();
  init();
  createScheduled('new', 5*1000, () => { init() })
});

const setInit = async () => {
  let tempList = []
  let tempParams1 = []
  let tempParams2 = []
  let tempParams4 = []
  let query_voltage = {"o": "o_wf_voltage"}
  let query_bay = {"o": "o_wf_bay" , "p": [publicStore.distributId]}
  let query1 = {"o": "o_sensor", "p": [publicStore.distributId], "i": 0}
  let query2 = {"o": "o_sensor_star", "p": [publicStore.distributId], i: 1}
  let query3 = {"o": "o_sensor_template_cfg_property", "p":[route.query.type], "i":0}
  let params = {queryApi: query_voltage, queryApii: query_bay, queryApi1: query1, queryApi2: query2, queryApi3: query3}
  let res = await publicStore.post(params)
  if(proxy.isNull(res)) return
  state.voltageList = proxy.isNull(res.queryApi)? [] : res.queryApi
  state.bayList = proxy.isNull(res.queryApii)? [] : res.queryApii
  state.templateValue = proxy.isNull(res.queryApi3)? [] : res.queryApi3
  res.queryApi1.filter(a=>a.class == '0' && a.type == route.query.type).forEach(v => {
    let exist = res.queryApi2.find(a=>a.sensor_id == v.id)
    let json_list = exist? {offline: exist.offline} : {offline: ''}
    let json_params1 = {"o":"o_r_sensor", "p":[v.id], "i": 1}
    let json_params2 = {"o":"o_sensor_cfg_property", "p":[v.id], "i": 0}
    let json_params4 = {"o": "o_sensor_alarm_log", "p": [publicStore.distributId, v.type], "i": 3}
    tempParams1.push(json_params1)
    tempParams2.push(json_params2)
    // if(json_params4.p[1].indexOf('PD') != -1 || json_params4.p[1].indexOf('pd') != -1) json_params4.p[1] = 'PD'
    if (!tempParams4.some(a => a.p[1] === v.type)) tempParams4.push(json_params4)
    tempList.push(Object.assign(v, json_list))
  })
  state.params1 = tempParams1
  state.params2 = tempParams2
  state.params4 = tempParams4
  publicStore._public.list = tempList
  configStore.bay = ''
  configStore.voltage = ''
  getBayList()
  getVoltageList()
};

const getBayList = () => {
  let tempArr = []
  publicStore._public.list.filter(a=>a.class == '0' && a.type == route.query.type).forEach(v => {
    if(v.bayNum){
      let exist = tempArr.find(a=>a.id == v.bayNum)
      if(!exist){
        let bay = state.bayList.find(a=>a.bayNum == v.bayNum)
        tempArr.push({voltage: bay.voltage, name: bay.bayName, id: v.bayNum, count: 1, offline: v.offline==1?1:0, alarm:v.isAlarm > 0?1:0})
      }else{
        exist.count ++
        if(v.offline==1) exist.offline ++
        if(v.isAlarm > 0) exist.alarm ++
      }
    }
  })
  if(tempArr.length == 0) configStore.bay = ''
  if(tempArr.length == 1) configStore.bay = tempArr[0].id
  publicStore._public.bayList = tempArr
}

const getVoltageList = () => {
  let tempArr = []
  publicStore._public.bayList.forEach(v => {
    let exist = tempArr.find(a=>a.id == v.voltage)
    if(!exist){
      let voltage = state.voltageList.find(a=>a.voltage == v.voltage)
      tempArr.push({id: v.voltage, name: voltage.name, count: v.count, offline: v.offline, alarm: v.alarm})
    }else{
      exist.count = exist.count +  v.count
      exist.offline = exist.offline +  v.offline
      exist.alarm = exist.alarm +  v.alarm
    }
  })
  if(tempArr.length == 0) configStore.voltage = ''
  if(tempArr.length == 1) configStore.voltage = tempArr[0].id
  publicStore._public.voltageList = tempArr
}

const init = async () => {
  getList()
  getAlarmList()
  getAlarmType()
};

const getList = async() => {
  let params = {queryApi4: state.params1, queryApi5: state.params2}
  let res = await publicStore.post(params)
  publicStore._public.list.forEach(v => {
    let r_sensor = res.queryApi4.find(a=>a.sensor_id == v.id)
    let cfg_sensor = res.queryApi5.filter(a=>a.sensor_id == v.id)
    let exist = publicStore._public.bayList.find(a=>a.id == v.bayNum)
    if(exist && r_sensor.offline == 1) exist.offline ++
    if(exist && r_sensor.isAlarm > 0) exist.alarm ++
    Object.assign(v, r_sensor, setCfg(cfg_sensor))
  });
  getCount()
  getBayList()
  getVoltageList()
  console.log('list------', publicStore._public.list)
  console.log('bayList------', publicStore._public.bayList)
  console.log('voltageList------', publicStore._public.voltageList)
}

const getCount = async() => {
  let count = {count: publicStore._public.list.length, isAlarm: 0, isNormal: 0, alarm_rate: 0, offline: 0, online: 0, online_rate: 0}
  publicStore._public.list.forEach(v => {
    if(v.offline){
      if(v.offline == 1) count.offline ++ 
      if(v.offline == 0) count.online ++ 
    }
    if(v.isAlarm){
      if(v.isAlarm != 0) count.isAlarm ++ 
      if(v.isAlarm == 0) count.isNormal ++ 
    }
  });
  count.alarm_rate = Math.round((count.isAlarm/count.count)*10000)/100
  count.online_rate = Math.round((count.online/count.count)*10000)/100
  console.log('count', count)
  publicStore._public.count = count;
};

const setCfg = (cfg_sensor) => {
  let vcArr = []
  let dwArr = []
  let temp = {}
  cfg_sensor.forEach(vv => {
    temp[vv.code] = vv.value
    let exist = state.templateValue.find(a=>a.code == vv.code)
    if(exist){
      if(vv.code.indexOf('vc_') != -1) vcArr.push({name: exist.name, code: vv.value})
      if(vv.code.indexOf('dw_') != -1) dwArr.push(vv.value)
    }
  });
  vcArr.forEach((vv, ii) => {
    vv.dw = dwArr[ii]
  });
  temp['values'] = vcArr
  return temp
}

const getAlarmList = async() => {
  let query = {"o": "o_sensor_alarm_log", "p": [publicStore.distributId, route.query.type], "i": 16, s: 0, n: 20}
  let params = {queryApi6: query}
  let res = await publicStore.post(params)
  publicStore._public.alarmList = proxy.isNull(res)? [] : res;
}

const getAlarmType = async() => {
  let params = {queryApi7: state.params4}
  let res = await publicStore.post(params)
  res.forEach(v => {
    v.name = v.alarm_name
    v.value = parseInt(v.alarm_count)
  });
  publicStore._public.alarmType = res;
}

</script>

<style lang="scss">

</style>
